<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use \dmstr\bootstrap\Tabs;
use yii\helpers\StringHelper;
use \trntv\filekit\widget\Upload;
use yii\bootstrap\Modal;
use yii\redactor;
use yii\helpers\Url;
/**
 * @var yii\web\View $this
 * @var yiike\goods\model\Goods $model
 * @var yii\widgets\ActiveForm $form
 */

?>

<div class="goods-form">

    <?php $form = ActiveForm::begin([
            'id' => 'Goods',
            'layout' => 'horizontal',
            'enableClientValidation' => true,
            'errorSummaryCssClass' => 'error-summary alert alert-danger',
            'fieldConfig' => [
                'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                'horizontalCssClasses' => [
                    'label' => 'col-sm-2',
                    #'offset' => 'col-sm-offset-4',
                    'wrapper' => 'col-sm-8',
                    'error' => '',
                    'hint' => '',
                ],
            ],
        ]
    );
    ?>
    <?php echo $form->errorSummary($model); ?>


    <div class="">
        <?php $this->beginBlock('basic'); ?>

        <p>

            <!-- attribute title -->
            <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
            <!-- attribute sub_title -->
            <?= $form->field($model, 'sub_title')->textInput(['maxlength' => true]) ?>

            <!-- attribute short_title -->
            <?= $form->field($model, 'short_title')->textInput(['maxlength' => true]) ?>

            <!-- attribute cid -->
            <?= $form->field($model, 'cid')->dropDownList($category)?>

            <?=
            $form->field($model, 'imgarr')->label('图片')->widget(
                Upload::className(), [
                    'url'              => ['/files/upload'],
                    'sortable'         => true,
                    'maxFileSize'      => 10000000, // 10 MiB
                    'maxNumberOfFiles' => 4,

                ]
            );
            ?>

            <!-- attribute price -->
            <?= $form->field($model, 'price')->textInput(['maxlength' => true]) ?>

            <!-- attribute orig_price -->
            <?= $form->field($model, 'orig_price')->textInput(['maxlength' => true]) ?>

            <!-- attribute cost_price -->
            <?= $form->field($model, 'cost_price')->textInput(['maxlength' => true]) ?>

            <!-- attribute sold -->
            <?= $form->field($model, 'sold')->textInput() ?>


            <?=
            $form->field($model, '地区')->widget(\chenkby\region\Region::className(),[
                'model'=>$model,
                'url'=> \yii\helpers\Url::toRoute(['get-region']),
                'province'=>[
                    'attribute'=>'province',
                    'items'=> \common\models\Region::getRegion(),
                    'options'=>['class'=>'form-control col-lg-3','prompt'=>'选择省份', 'style' => 'width:150px;']
                ],
                'city'=>[
                    'attribute'=>'city',
                    'items'=>\common\models\Region::getRegion($model['province']),
                    'options'=>['class'=>'form-control col-lg-3','prompt'=>'选择城市', 'style' => 'width:150px;']
                ],
                'district'=>[
                    'attribute'=>'district',
                    'items'=>\common\models\Region::getRegion($model['city']),
                    'options'=>['class'=>'form-control col-lg-3','prompt'=>'选择县/区', 'style' => 'width:150px;']
                ]
            ]);
            ?>
            <!-- attribute invoice -->
            <?= $form->field($model, 'invoice')->dropDownList(['1'=>'有', '0'=>'无']); ?>

            <!-- attribute status -->
            <?= $form->field($model, 'status')->dropDownList(['1'=>'上架', '0'=>'下架']); ?>


        </p>
        <?php $this->endBlock(); ?>
        <?php $this->beginBlock('sku'); ?>
        <p>

            <!-- attribute goods_sn -->
            <?= $form->field($model, 'goods_sn')->textInput(['maxlength' => true, 'class' => 'form-control stock']) ?>

            <?= $form->field($model, 'weight')->textInput(['maxlength' => true, 'class' => 'form-control stock']) ?>

            <?= $form->field($model, 'stock')->textInput(['maxlength' => true, 'class' => 'form-control stock']) ?>


            <div class="form-group" >
                <label class="control-label col-sm-2" for="goods-stock"></label>
                <div class="col-sm-8">
        <p class="help-block">启用多规格后，上述设置无效.</p>
    </div>
</div>
<!-- attribute stock_rule -->
<?= $form->field($model, 'stock_rule')->radioList(['1'=>'拍下减库存','2'=>'付款减库存','3'=>'永不减库存'],['class'=>'label-group'])->label('减库存规则'); ?>

<!-- attribute is_spec -->
<div class="form-group">
    <label class="col-xs-12 col-sm-3 col-md-2 control-label">是否启用多规格</label>
    <div class="col-sm-4">
        <label  class="checkbox-inline">
            <input name='is_spec' type="checkbox" value="1" <?php if($is_spec==1){ echo 'checked';}?>/>启用

        </label>
        <label class="checkbox-inline"><a  class="btn-info btn add_value" style="display:none">添加多规格</a></label>
    </div>
</div>
<div id="spec_val" style="display:none;">
    <div class="form-group">
        <label class="col-xs-12 col-sm-3 col-md-2 control-label">规格值</label>
        <div class="col-md-8">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th width="10%">规格名 *</th>
                    <th>售价 *</th>
                    <th>原价</th>
                    <th>成本价</th>
                    <th>库存</th>
                    <th>编码</th>
                    <th>重量(千克)</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="spec_add">
                <?php if($SpecData){?>
                    <?php foreach($SpecData as $index=>$row){ ?>
                        <tr>
                            <td>
                                <input type="hidden" class="form-control"  name="spec_id[]"  value="<?php echo $row['id']; ?>"  />
                                <input type="text" class="form-control"  name="spec_name[]"  value="<?php echo $row['name']; ?>" required  />
                            </td>
                            <td>
                                <input type="number" min="0.0" step="0.01"  class="form-control" name="spec_price[]"  value="<?php echo $row['price'];?>" required />
                            </td>
                            <td>
                                <input class="form-control" type="number" min="0.0" step="0.01"  name="spec_orig_price[]"  value="<?php echo $row['orig_price'];?>"  />
                            </td>
                            <td>
                                <input class="form-control" type="number" min="0.0" step="0.01"  name="spec_cost_price[]"  value="<?php echo $row['cost_price']?>"  />
                            </td>
                            <td>
                                <input class="form-control" type="number"  name="spec_stock[]"  value="<?php echo $row['stock']?>"  />
                            </td>
                            <td>
                                <input class="form-control" type="text"  name="spec_goods_sn[]"  value="<?php echo $row['goods_sn'];?>"  />
                            </td>
                            <td>
                                <input class="form-control" type="number" min="0.0" step="0.01"  name="spec_weight[]"  value="<?php echo $row['weight']?>"  />
                            </td>
                            <td>
                                <a href="javascript:;" class='btn-info btn del_spec ajax_del' url="<?php echo Url::to(['goods/spec-del', 'id' => $row['id']]); ?>" title="删除" aria-label="删除" data-pjax="0"  data-method="post">删除</a>
                                <!-- <a  class='btn-info btn del_spec ajax_del' data-id="<?php /*echo $row['id']*/?>" >删除</a>-->
                            </td>
                        </tr>
                    <?php } ?>
                <?php } ?>

                </tbody>
            </table>
        </div>
    </div>
</div>
</p>
<?php $this->endBlock(); ?>
<?php $this->beginBlock('info'); ?>
<p>
    <!-- attribute content -->
    <?php $model->content = Html::decode($model->content); ?>
    <?= $form->field($model,'content')->widget('kucha\ueditor\UEditor',[]); ?>
</p>
<?php $this->endBlock(); ?>

<?=
Tabs::widget(
    [
        'encodeLabels' => false,
        'items' => [
            [
                'label'   => Yii::t('models', '基本信息'),
                'content' => $this->blocks['basic'],
                'active'  => true,
            ],
            [
                'label'   => Yii::t('models', '库存规格'),
                'content' => $this->blocks['sku'],
                'active'  =>false,
            ],
            [
                'label'   => Yii::t('models', '商品描述'),
                'content' => $this->blocks['info'],
                'active'  =>false,
            ]
        ]
    ]
);
?>
<hr/>

<?php ActiveForm::end(); ?>

<?php $this->beginBlock('goodsEdit') ?>
var isChecked = $("input[name='is_spec']").is(":checked");
if(isChecked){
$('.stock').attr('disabled',true);
$('#spec_val').css('display','block');
$('.add_value').css('display','block');
}else{
$('.stock').attr('disabled',false);
$('#spec_val').css('display','none');
$('.add_value').css('display','none');
}
$('#setArea').unbind('click').click(function(){
$('#goods-no_shipping').val('');
citystrs = '';
})
$('#btnSubmitArea').unbind('click').click(function(){
$('#goods-no_shipping').val('');
citystrs = ''
$('.cityall:checked').each(function(){

citystrs+= $(this).val() +",";
});
$('#goods-no_shipping').val(citystrs);
})
//多规格
$("input[name='is_spec']").click(function(){
var isChecked = $(this).is(":checked");
if(isChecked){
$('.stock').attr('disabled',true);
$('#spec_val').css('display','block');
$('.add_value').css('display','block');
}else{
$('.stock').attr('disabled',false);
$('#spec_val').css('display','none');
$('.add_value').css('display','none');
}
})

//添加规格
$('.add_value').click(function(){
var html = "<tr>";
    html+="<td><input type='text' class='form-control'  name='spec_name[]' required /> </td>" ;
    html+="<td><input type='number' min=\"0.0\" step=\"0.01\" class='form-control' name='spec_price[]' required /></td>";
    html+="<td><input type='number' min=\"0.0\" step=\"0.01\" class='form-control' name='spec_orig_price[]'  /></td>";
    html+="<td><input type='number' min=\"0.0\" step=\"0.01\" class='form-control' name='spec_cost_price[]'  /></td>";
    html+="<td><input type='number' class='form-control' name='spec_stock[]'  /></td>";
    html+="<td><input type='text' class='form-control' name='spec_goods_sn[]'  /></td>";
    html+="<td><input type='number' min=\"0.0\" step=\"0.01\" class='form-control' name='spec_weight[]'  /></td>";
    html+="<td><a  class='btn-info btn del_spec'>删除</a></td>";
    html+="</tr>";
$('#spec_add').append(html);
})

//删除规格
$('tbody').on('click','.del_spec',function(){
$(this).parents('tr').remove();
});
//添加参数
$('.add_param').click(function(){
var html = "<div class='form-group'>";
    html+="<label class='col-xs-12 col-sm-3 col-md-2 control-label'></label>";
    html+="<div class='col-sm-9'>";
        html+="<div class='col-sm-3'><input type='text' name='param_name[]' placeholder='参数名称'  class='form-control'  /> </div>" ;
        html+="<div class='col-sm-3'><input type='text' name='param_val[]' placeholder='参数值' class='form-control'/></div>";
        html+="<a class='del_param btn-default btn' href='javascript:;'>删除</a>";
        html+="</div></div>";
$('#w2-tab5').append(html);
})
//删除参数
$('#w2-tab5').on('click','.del_param',function(){
$(this).parents('.form-group').remove();
});

$(function () {
$('.ajax_del').click(function () {
var tThis =$(this);
if (confirm("确定要删除此项规格？")){
var url = tThis.attr('url');
$.get(url,function (data) {
if (data == true){
$(Tthis).parents('tr').remove();
alert('删除成功');
}else{
alert('删除失败');
}
})
}else{
return false;
}

})
})
<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['goodsEdit']); ?>

<script>


</script>
</div>

</div>

